package utility;

/**
 * A class representing a word object.  Stores a string representation of the word 
 * and a counter representing the number of times the word has occurred.
 * 
 * @author Dallas Rosson (dallasjrosson@gmail.com)
 * @version 1.0
 */
public class Word implements Comparable<Word> {
	
	/**
	 * The string representation of the word.
	 */
	private final String myWord;
	
	/**
	 * The word occurrence count.
	 */
	private int myCount = 0;
	
	/**
	 * Creates a word.
	 * 
	 * @param the_word The string representation of the word.
	 */
	public Word(final String the_word) {
		
		myWord = the_word;
		myCount = 1;
	}
	
	// Getters and Setters
	
	/**
	 * Adds one to the words count value.
	 */
	public void addCount() {
		
		myCount++;
	}
	
	/**
	 * Get the number of times the word appears.
	 * 
	 * @return the count as an integer.
	 */
	public int getCount() {
		
		return myCount;
	}
	
	// Utility methods.
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		
		return myWord.toString();
	}
	
	@Override
	public int hashCode() { // Eclipse generated hash code.
		
		final int prime = 31;
		int result = 1;
		result = prime * result + ((myWord == null) ? 0 : myWord.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object the_o) {
		
		if (this == the_o)
			
			return true;
		
		if (the_o == null)
			
			return false;
		
		if (getClass() != the_o.getClass())
			
			return false;
		
		Word other = (Word) the_o;
		
		if (myWord == null) {
			
			if (other.myWord != null)
				
				return false;
			
		} else if (!myWord.equals(other.myWord))
			
			return false;
		
		return true;
	}

	/**
	 * Compares this Word to the passed in Word.
	 * 
	 * @return -1 if this Word is less than the passed in Word. <br>
	 * 0 if this Word is equal to the passed in Word.<br>
	 */
	@Override
	public int compareTo(final Word the_word) {
		
		return this.toString().compareTo(the_word.toString());
	}
	

}
